﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：SecurityConfig.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年6月9日星期六
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ajayumi.Platform.Core.Security
{
    /// <summary>
    /// SecurityConfig
    /// </summary>
    internal class SecurityConfig
    {

        private static object m_lockObj = new object();

        public const string FILE_NAME = "SecurityCfg.config";
        internal const string SECURITY_CONFIGURATION_KEY = "securityManager";


        private static SecurityManagerSection m_config = null;
        public static SecurityManagerSection Config
        {
            get
            {
                if (m_config == null)
                {
                    lock (m_lockObj)
                    {
                        if (m_config == null)
                        {
                            ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
                            filemap.ExeConfigFilename = FILE_NAME;//配置文件路径
                            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);

                            m_config = (SecurityManagerSection)config.GetSection(SECURITY_CONFIGURATION_KEY);

                            if (m_config == null)
                            {
                                m_config = (SecurityManagerSection)ConfigurationManager.GetSection(SECURITY_CONFIGURATION_KEY);
                            }

                            //m_config = (SecurityManagerSection)ConfigurationManager.GetSection("securityManager");
                        }
                    }
                }

                return m_config;
            }
        }

        /// <summary>
        /// 加密/解密 是否可用
        /// </summary>
        public static bool CryptoEnabled
        {
            get
            {
                return Config.Crypto.Enabled;
            }
        }

        /// <summary>
        /// 加密/解密 的密钥，对称加密下使用
        /// </summary>
        public static string CryptoKey
        {
            get
            {
                return Config.Crypto.Key;
            }
        }

        /// <summary>
        /// 公钥文件路径，非对称加密下使用
        /// </summary>
        public static string XmlPublicKey
        {
            get { return Config.Crypto.XmlPublicKey; }
        }

        /// <summary>
        /// 私钥文件路径，非对称加密下使用
        /// </summary>
        public static string XmlPrivateKey
        {
            get { return Config.Crypto.XmlPrivateKey; }
        }
    }
}
